<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\ProductModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ProductModelController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ProductModel(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('field_type');
            $grid->column('model_type');
            $grid->column('default_value');
            $grid->column('do_before');
            $grid->column('do_after');
            $grid->column('rules');
            $grid->column('explain');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ProductModel(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('field_type');
            $show->field('model_type');
            $show->field('default_value');
            $show->field('do_before');
            $show->field('do_after');
            $show->field('rules');
            $show->field('explain');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ProductModel(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $field_types = [
                'text' => '文本',
                'textarea' => '文本域',
                'number' => '数字',
                'select' => '下拉选择',
                'radio' => '单选',
                'checkbox' => '多选',
                'date' => '日期',
                'datetime' => '日期时间',
                'time' => '时间',
                'email' => '邮箱',
                'url' => 'URL',
                'ip' => 'IP',
                'file' => '文件',
                'image' => '图片',
                'password' => '密码',
                'hidden' => '隐藏',
            ];
            $form->select('field_type')->options($field_types);
            $model_types = [
                'toy' => '玩具摆件',
                'asp' => '汽配',
                'tool' => '五金工具',
                'lighting' => '灯具照明',
                'home' => '家居',
                'other' => '其他',
            ];
            $form->select('model_type')->options($model_types);
            $form->text('default_value');
            $form->text('do_before');
            $form->text('do_after');
            $form->text('rules')->placeholder('例如：required|string|min:3|regex:/^\d+$/|max:10');
            $form->text('explain');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
